//
//  NextViewController.m
//  09-省市区tableView
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "NextViewController.h"
#import "NSZoneViewController.h"

@interface NextViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<NSMutableDictionary *> *country;

@property (nonatomic, strong) NSMutableArray *cityAndProvinceArray;
@property (nonatomic, strong) NSMutableArray<NSMutableDictionary *> *cityAndProvinceDics;

@property (nonatomic, strong) NSMutableArray<NSMutableDictionary *> *cityArray;

@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor purpleColor];
    self.navigationItem.title = @"省份-市区";
    self.navigationController.navigationBar.translucent = NO;
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    
    [self createData];

}
- (void)createData {
    NSString *path = @"/Users/dllo/DLS160710/UI/HomeWork/09-省市区tableView/09-省市区tableView/area.txt";
    NSString *fileStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    //    NSLog(@"%@",fileStr);
    NSArray *dataArray = [fileStr componentsSeparatedByString:@"\n"];
    //    NSLog(@"%@",dataArray);
    self.country = [NSMutableArray array];
    _cityAndProvinceArray = [NSMutableArray array];
    _cityArray = [NSMutableArray array];
    _cityAndProvinceDics = [NSMutableArray array];

    for (NSString *line in dataArray) {
        if (![line hasPrefix:@" "]) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            NSMutableArray *arr = [NSMutableArray array];
            [dic setObject:line forKey:@"省名"];
            [dic setObject:arr forKey:@"市组"];
            [_country addObject:dic];
            [_cityAndProvinceArray addObject:line];
            [_cityAndProvinceDics addObject:dic];

        } else if (![line hasPrefix:@"   "]) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            NSMutableArray *arr = [NSMutableArray array];
            [dic setObject:line forKey:@"市名"];
            [dic setObject:arr forKey:@"区组"];
            NSMutableArray *cityArr = [[_country lastObject] objectForKey:@"市组"];
            [cityArr addObject:dic];
            [_cityAndProvinceArray addObject:line];
            [_cityArray addObject:dic];
            [_cityAndProvinceDics addObject:dic];

        } else {
            NSMutableArray *cityArr = [[_country lastObject] objectForKey:@"市组"];
            NSMutableArray *zoneArr = [[cityArr lastObject] objectForKey:@"区组"];
            [zoneArr addObject:line];
        }
    }

        NSLog(@"%@",_cityArray);
    NSLog(@"%ld",_country.count);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _cityAndProvinceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
   
    }
    NSArray *tempArr =[_cityAndProvinceArray[indexPath.row] componentsSeparatedByString:@" "];
    if (![_cityAndProvinceArray[indexPath.row] hasPrefix:@" "]) {
        ;
        cell.textLabel.text = [tempArr firstObject];
        cell.detailTextLabel.text = nil;
    } else {
        
        cell.textLabel.text = tempArr[2];
        cell.detailTextLabel.text = [tempArr lastObject];
    }
    
   
    return cell;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_cityAndProvinceArray[indexPath.row] isEqualToString:[_cityAndProvinceDics[indexPath.row] objectForKey:@"市名"]]) {
        NSZoneViewController *nsZoneVC = [NSZoneViewController new];
        [self.navigationController pushViewController:nsZoneVC animated:YES];
        nsZoneVC.zoneArr = [_cityAndProvinceDics[indexPath.row] objectForKey:@"区组"];
        NSArray *tempArr =[_cityAndProvinceArray[indexPath.row] componentsSeparatedByString:@" "];
        nsZoneVC.titleStr = [tempArr firstObject];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
